//
//  ViewController2.m
//  03-27-JSON&XML-练习
//
//  Created by student on 16/3/27.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController2.h"
#import "Weather.h"
@interface ViewController2 ()<UITableViewDataSource,UITableViewDelegate,NSXMLParserDelegate>
{
    NSString *curString;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(strong,nonatomic)NSMutableArray *list;
@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
  
    // 练习
    // 1. 制作一个全国天气应用
    // http://flash.weather.com.cn/wmaps/xml/china.xml
    //
    // 第一页面显示全国省天气列表(表格)
    // 点省后，显示这个省里城市的天气信息列表
    // 点城市后,显示这个城市下区的信息列表
    
    // 2. 解析NotesTestData.xml,用表格显示出来
    
    
    //1.获取数据
    NSString *urlStr=@"http://flash.weather.com.cn/wmaps/xml/china.xml";
    NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
    
    //2.创建一个解析器
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
    //代理人
    
    parser.delegate=self;
    
    //开始解析
    [parser parse];
    
    UISwipeGestureRecognizer *rightR=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(back)];
    
    rightR.direction=UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightR];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    
    Weather *weather =self.list[indexPath.row];
    cell.textLabel.text=weather.quName;
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@:%@,%@~%@度,%@",weather.cityname,weather.stateDetailed,weather.tem1,weather.tem2,weather.windState];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Weather *weather=self.list[indexPath.row];
    curString=weather.pyName;
    if (![weather.pyName isEqualToString:@""]) {
        NSMutableString *urlStr=[@"http://flash.weather.com.cn/wmaps/xml/china.xml" mutableCopy];
        NSRange range=[urlStr rangeOfString:@"china"];
        [urlStr replaceCharactersInRange:range withString:weather.pyName];
    
    NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
    //代理人
    
    parser.delegate=self;
    
    //开始解析
    [parser parse];

    }
}

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    self.list=[NSMutableArray array];
}

- (void)parserDidEndDocument:(NSXMLParser *)parser;
{
    [self.tableView reloadData];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(nullable NSString *)namespaceURI qualifiedName:(nullable NSString *)qName attributes:(NSDictionary<NSString *, NSString *> *)attributeDict
{
    if ([elementName isEqualToString:@"city"]) {
        
        Weather *weather=[[Weather alloc]initWithDict:attributeDict];
       
        [self.list addObject:weather];
        
    }
}
-(void)back
{
 
    NSString *urlStr=[NSString stringWithFormat:@"http://flash.weather.com.cn/wmaps/xml/%@.xml",curString];
       NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:urlStr]];
    NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
    //代理人
    
    parser.delegate=self;
    
    //开始解析
    [parser parse];

}

@end
